﻿using EECMISWS;
using KWindow.KPageCore;
using LogLib;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YSLPages
{
    public partial class PageStarting : PageControlBase
    {      
        /// <summary>
        /// 连续点击激活次数统计
        /// </summary>
        private int active_click_count = 0;

        private Stopwatch sw = new Stopwatch();

        private PictureBox pictureBox1 = null;

        private Button buttonStart = null;
        private Label labelVersion = null;

        public PageStarting()
        {
            InitializeComponent();
            SetProcessBase(ProcessStarting.Instance);
        }

        private void GetControls()
        {
            pictureBox1 = GetControl<PictureBox>("pictureBox1") as PictureBox;
            buttonStart = GetControl<Label>("buttonStart") as Button;
            labelVersion = GetControl<Label>("labelVersion") as Label;
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            GetControls();
            SetButtonDefaultStyles(buttonStart);           
        }

        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            SD.ClearValue();

            if (labelVersion != null)
            {
                string version = "版本号: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                labelVersion.Text = version;
            }
                
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            base.UpdateData();
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        public override void StartWork()
        {
            base.StartWork();
            WebDataAccess webAccess = WebDataAccess.Instance;
            Task.Factory.StartNew(() =>
            {
                if (webAccess.Enabled)
                {
                    if (!webAccess.Login())
                    {
                        string strLog = "系统登录失败:" + webAccess.ErrorMessage;
                        Log.WriteAsync(strLog);
                        ShowLoginFailedMessage(webAccess.ErrorMessage);
                    }
                }
            });
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        private void ShowLoginFailedMessage(string error)
        {
            //Action<string> handler = (e) =>
            //{
            //    MessageBox.Show(e, "全国版系统登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //};
            //this.Invoke(handler, error);
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            base.btnNext_Click(sender, e);
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            base.btnCancel_Click(sender, e);
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            Process.ToNext();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (!sw.IsRunning)
            {
                sw.Restart();
                return;
            }
            if (sw.ElapsedMilliseconds > 2000)
            {
                sw.Reset();
                active_click_count = 0;
            }
            else
            {
                active_click_count++;
            }
            if (active_click_count == 2)
            {
                sw.Reset();
                active_click_count = 0;
                Log.WriteAsync("用户打开了管理员选项");
                FormAdministrator form = new FormAdministrator();
                if (form.ShowDialog() == DialogResult.OK)
                {
                    Log.WriteAsync("即将执行管理员选项动作:" + form.SelectAction.ToString());
                    Process.OnTakeAdminAction(form.SelectAction);
                }
            }
        }

    }
}
